package com.candy.actiivemq.jms;

import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author: candy
 * @date: 2020/9/20
 * @description :
 **/
@Slf4j
public class JmsConsumer {

    private final static String URL = "tcp://192.168.0.108:61616";

    public static void main(String[] args) throws JMSException {

        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(URL);
        Connection connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue syncQueue = session.createQueue("syncQueue");

        MessageConsumer consumer = session.createConsumer(syncQueue);

        while (true) {
            Message receive = consumer.receive();
            if (receive != null && receive instanceof TextMessage) {
                TextMessage message = (TextMessage) receive;
                String text = message.getText();
                log.info("接收到 生产者的消息 : {}",  text);
            }else {
                break;
            }
        }
        consumer.close();

        session.close();

        connection.close();

    }
}
